package com.cbank.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.cbank.domain.User;

@Repository
public interface UserDao {
	String ALL = "id,view_id,can_download_each_day,name,pwd,token,create_time,update_time,status,remark,balance";

	@Insert("insert into user(view_id,can_download_each_day,name,pwd,token,status,remark,balance) values("
			+ "#{viewId},#{canDownloadEachDay},#{name},#{pwd},#{token},#{status},#{remark},#{balance})")
	@Options(useGeneratedKeys = true)
    void insert(User obj);

	@Select("select " + ALL + " from user where id = #{id} and status = 1 limit 1")
    User getById(Long id);

	@Select("select " + ALL + " from user where name = #{name} and pwd = #{pwd} and status = 1 limit 1")
    User getByNamePwd(@Param(value = "name") String name, @Param(value = "pwd") String pwd);

	@Select("select " + ALL + " from user where view_id = #{viewId} and status = 1 limit 1")
    User getByViewId(Long viewId);

	@Select("select " + ALL + " from user where view_id = #{viewId} and token = #{token} and status = 1 limit 1")
    User getByViewIdAndToken(@Param(value = "viewId") long viewId, @Param(value = "token") String token);

	@Update("update user set can_download_each_day = #{canDownloadEachDay},name = #{name},pwd = #{pwd},token = #{token},status = #{status},remark = #{remark},balance = #{balance} where id = #{id} limit 1")
    void update(User o);

	@Select("select " + ALL + " from user where status = 1 order by create_time DESC limit #{index},#{offset}")
    List<User> getPage(@Param(value = "index") int index, @Param(value = "offset") int offset);

	@Select("select count(1) from user where status = 1")
    int getCount();
}
